package cn.exrick.manager.service.hotclassload;

import org.apache.commons.lang3.ArrayUtils;

public class TestSort {


    public static int[] selectSort(int[] targetArr){
        if(ArrayUtils.isEmpty(targetArr)){
            return null;
        }
        int size = targetArr.length;
        int flag;
        for(int i = 0;i<size;i++){
            int min = targetArr[i];
            flag = i;
            for(int j = i+1;j<size;j++){
                if(targetArr[j]<min){
                    min = targetArr[j];
                    flag = j;
                }
            }
            if(flag != i){
               targetArr[flag] = targetArr[i];
                targetArr[i]=min;
            }

        }
        return targetArr;
    }


    public static void selectSort2(int[] target){
        if (ArrayUtils.isEmpty(target)) {
            return;
        }
        int size = target.length;
        int minPoint;
        int maxPoint;
        int temp;
        int len = size >>1;
        for(int i = 0;i<len;i++){
            minPoint = i;
            maxPoint = i;
            for(int j = i+1;j<size-i;j++){
                if(target[j]<target[minPoint]){
                    minPoint = j;
                    continue;
                }else if(target[j]> target[maxPoint]){
                    maxPoint = j;
                }
            }
            //交换最小值
            if(minPoint != i){
                temp = target[i];
                target[i]=target[minPoint];
                target[minPoint] = temp;
                if(maxPoint == i){
                    maxPoint=minPoint;
                }
            }
            //交换最大值
            if(maxPoint != size-1-i){
                temp = target[size-1-i];
                target[size-1-i] = target[maxPoint];
                target[maxPoint] = temp;
            }
        }
    }

    public static int[] selectSort3(int[] arr){
        int minPoint;  //存储最小元素的小标
        int maxPoint;  //存储最大元素的小标
        int len = arr.length;
        int temp;
        //只需要跑N/2趟即可
        for(int i=0;i<len>>1;i++){
            minPoint= i;
            maxPoint= i;
            for(int j=i+1;j<=len-1-i;j++){
                //每一趟的最小值
                if(arr[j]<arr[minPoint]){
                    minPoint= j;
                    continue;
                }
                //每一趟的最小值
                else if(arr[j]>arr[maxPoint]){
                    maxPoint= j;
                }
            }
            //将最小值与前面的值交换
            if(minPoint!=i){
                temp= arr[i];
                arr[i]= arr[minPoint];
                arr[minPoint]= temp;
                if(maxPoint== i){
                    maxPoint= minPoint;
                }

            }
            //将最大值与后面的值交换
            if(maxPoint!=len-1-i){
                temp= arr[len-1-i];
                arr[len-1-i]= arr[maxPoint];
                arr[maxPoint]= temp;
            }
        }
        return arr;
    }

   /* public static void main(String[] args) {
        int[] arr = new int[]{2,13,18,15,19,14,5,16,17,12,3,4};
        //System.out.println("排序后："+ Arrays.toString(selectSort(arr)));
        selectSort2(arr);
        System.out.println("排序后："+Arrays.toString(arr));
    }*/
}
